/***************************************************************************************************
Copyright 2009 - 2010 Harshana Eranga Martin, Dishan Metihakwala, Rajeev Sampath, Chathura Randika

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
****************************************************************************************************/
package org.epzilla.client.userInterface;

import org.epzilla.client.controlers.ClientUIControler;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Login extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    private JLabel lblUName = null;
    private JLabel lblPassword = null;
    private JTextField tbUName = null;
    private JPasswordField tbPassword = null;
    private JLabel lblDetails = null;
    private JButton btnOK = null;
    private JButton btnCancel = null;
    private JButton btnClose = null;

    public Login() {
        super();
        initialize();
    }

    private JTextField getTbUName() {
        if (tbUName == null) {
            tbUName = new JTextField(25);
            tbUName.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
            tbUName.setSize(new Dimension(220, 20));
            tbUName.setLocation(new Point(120, 55));
        }
        return tbUName;
    }

    private JPasswordField getTbPassword() {
        if (tbPassword == null) {
            tbPassword = new JPasswordField(25);
            tbPassword.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
            tbPassword.setSize(new Dimension(220, 20));
            tbPassword.setLocation(new Point(122, 98));
        }
        return tbPassword;
    }

    private JButton getBtnOK() {
        if (btnOK == null) {
            btnOK = new JButton();
            btnOK.setLocation(new Point(179, 133));
            btnOK.setText("Ok");
            btnOK.setSize(new Dimension(75, 20));
            btnOK.addActionListener(this);
        }
        return btnOK;
    }

    private void showMainUI() {
        new ClientUIControler();
        ClientUIControler.initializeClientUI();
    }

    private JButton getBtnCancel() {
        if (btnCancel == null) {
            btnCancel = new JButton();
            btnCancel.setLocation(new Point(264, 133));
            btnCancel.setText("Cancel");
            btnCancel.setPreferredSize(new Dimension(75, 26));
            btnCancel.setSize(new Dimension(75, 20));
            btnCancel.addActionListener(this);
        }
        return btnCancel;
    }

    private JButton getBtnClose() {
        if (btnClose == null) {
            btnClose = new JButton();
            btnClose.setBounds(new Rectangle(322, 3, 25, 22));
            btnClose.setIcon(new ImageIcon("images\\iconClose.JPG"));
            btnClose.addActionListener(this);
        }
        return btnClose;
    }

    private void initialize() {
        lblDetails = new JLabel();
        lblDetails.setBounds(new Rectangle(142, 5, 55, 23));
        lblDetails.setFont(new Font("Dialog", Font.BOLD, 18));
        lblDetails.setText("Login");
        lblPassword = new JLabel();
        lblPassword.setText("Password :");
        lblPassword.setLocation(new Point(27, 98));
        lblPassword.setSize(new Dimension(80, 20));
        lblUName = new JLabel();
        lblUName.setText("User Name :");
        lblUName.setSize(new Dimension(80, 20));
        lblUName.setLocation(new Point(27, 57));

        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setSize(new Dimension(350, 175));
        panel.setEnabled(true);
        panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        panel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        panel.add(lblUName, null);
        panel.add(lblPassword, null);
        panel.add(getTbUName(), null);
        panel.add(getTbPassword(), null);
        panel.add(lblDetails, null);
        panel.add(getBtnOK(), null);
        panel.add(getBtnCancel(), null);
        panel.add(getBtnClose(), null);
        int width = 350;
        int height = 175;
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screen.width - width) / 2;
        int y = (screen.height - height) / 2;
        setBounds(x, y, width, height);
        this.setUndecorated(true);
        this.getContentPane().add(panel);
        setVisible(true);
        this.setTitle("login");
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();
        if (source == btnCancel) {
            tbUName.setText("");
            tbPassword.setText("");
        } else if (source == btnClose) {
            System.exit(0);
        } else if (source == btnOK) {
            showMainUI();
            this.setVisible(false);
        }
    }
}
